const temp = {} // 当前重复的值,支持多列
const mergeCellKey = (text, array, columns) => {
  let i = 0

  if (columns.indexOf('.') > 0) {
    let columns1 = columns.split('.')[0]
    let columns2 = columns.split('.')[1]
    if(Object.values(temp).length == 0) return
    if (text !== temp[columns1][columns2]) {
      temp[columns1][columns2] = text
      array.forEach((item) => {
        if (item[columns1][columns2] === temp[columns1][columns2]) {
          i += 1
        }
      })
    }
  } else { 
    if (text !== temp[columns]) {
      temp[columns] = text
      array.forEach((item) => {
        if (item[columns] === temp[columns]) {
          i += 1
        }
      })
    }
  }

  
  return i
}

// 排序
/**
 * 重新排序
 * data: 数据源
 * targe: 数据源的唯一key值，需要合并的
 */
const sortData = (data, targe) =>{
  const newArr = [...new Set(data.map(i => i[targe]))];
  data.sort((x,y)=>{
      return newArr.indexOf(x[targe]) - newArr.indexOf(y[targe])
  })
  return Object.assign([], data)
}

// 获取去重统计数据
const filterData = (data, target) => { 
  const newArr = [...new Set(data.map(i => i[target]))];
  let list = [];
  newArr.forEach(i => {
      list.push(data.filter(t => t[target] === i));
  })
  let mlist = [];
  list.forEach((i, index) => {
    mlist.push({
        name: newArr[index],
        num: i.length,
    })
  }) 
  return mlist
}
// 合并数据
const mergeData = (list, target, chitarget) => {
  let newRrr = []
  list.forEach(item=>{
    let index = newRrr.findIndex(o=>{
        return o[target] === item[target]
    })
    if(index < 0){
      newRrr.push(item)
    }else{
      newRrr[index][chitarget] = [...newRrr[index][chitarget], ...item[chitarget]]
    }
  })    
  return newRrr 
}
export { mergeCellKey, sortData, filterData, mergeData  }